#include "server.h"

/* our database data containers */
/* DataBlock dbase; */ 
FILE *fstor;
const BDSIZE=50;

void db_open()
{
	char rbuf[255];
	ARecord temp;
	int counter;
	dbase.status=0;
	dbase.pos=-1;

	fstor=fopen("./Request","r+");
	
	counter=0;
	while(fgets(rbuf,255,fstor)!=NULL)
	{
		char* separator;
		separator=strpbrk(rbuf,":");
		separator+=2;
		switch(counter)
		{
			case 0:
				temp.date=(char*)malloc(strlen(separator));
				strncpy(temp.date,separator,strlen(separator)-1);
				counter++;
				break;
			case 1:
				temp.requestor=(char*)malloc(strlen(separator));
				strncpy(temp.requestor,separator,strlen(separator)-1);
				counter++;
				break;
			case 2:
				temp.email=(char*)malloc(strlen(separator));
				strncpy(temp.email,separator,strlen(separator)-1);
				counter++;
				break;
			case 3:
				temp.priority=(char*)malloc(strlen(separator));
				strncpy(temp.priority,separator,strlen(separator)-1);
				counter++;
				break;
			case 4:
				temp.needed=(char*)malloc(strlen(separator));
                strncpy(temp.needed,separator,strlen(separator)-1);
				counter++;
				break;
			case 5:
				temp.status=(char*)malloc(strlen(separator));
				strncpy(temp.status,separator,strlen(separator)-1);
				counter++;
				break;
			case 6:
				temp.description=(char*)malloc(strlen(separator));
				strncpy(temp.description,separator,strlen(separator)-1);
				counter++;
				break;
		}
		
		if (counter==7)		
		{
			db_add_record(temp,0);
			counter=0;
			free(temp.email);
			free(temp.requestor);
			free(temp.date);
			free(temp.priority);
			free(temp.needed);
			free(temp.status);
			free(temp.description);
		}
	}
	fclose(fstor);
}

/* if second parameter is 1 update file also */
int db_add_record(ARecord newrec, int fW)
{
	/* find free place in the list */
	int cur,i;
	if (dbase.pos<(BDSIZE-1))
	{
		dbase.pos++;
		cur=dbase.pos;
		
		dbase.data[cur].date=(char*)malloc(strlen(newrec.date));
		strcpy(dbase.data[cur].date,newrec.date);
		dbase.data[cur].requestor=(char*)malloc(strlen(newrec.requestor));
		strcpy(dbase.data[cur].requestor,newrec.requestor);
		dbase.data[cur].email=(char*)malloc(strlen(newrec.email));
		strcpy(dbase.data[cur].email,newrec.email);
		dbase.data[cur].priority=(char*)malloc(strlen(newrec.priority));
		strcpy(dbase.data[cur].priority,newrec.priority);
		dbase.data[cur].needed=(char*)malloc(strlen(newrec.needed));
		strcpy(dbase.data[cur].needed,newrec.needed);
		dbase.data[cur].status=(char*)malloc(strlen(newrec.status));
		strcpy(dbase.data[cur].status,newrec.status);
           	dbase.data[cur].description=(char*)malloc(strlen(newrec.description));
	        strcpy(dbase.data[cur].description,newrec.description);
			
		chomp(dbase.data[cur].date);
		chomp(dbase.data[cur].requestor);
		chomp(dbase.data[cur].email);
		chomp(dbase.data[cur].priority);
		chomp(dbase.data[cur].needed);
		chomp(dbase.data[cur].status);
		chomp(dbase.data[cur].description);		
		printf("Current!!!\n");
		printf("date: %s!\n", dbase.data[cur].date);
		printf("requestor: %s!\n", dbase.data[cur].requestor);
		printf("email: %s!\n", dbase.data[cur].email);
		printf("priority: %s!\n", dbase.data[cur].priority);
		printf("needed: %s!\n", dbase.data[cur].needed);
		printf("status: %s!\n", dbase.data[cur].status);
		printf("description: %s!\n", dbase.data[cur].description);
		/*
			for (i=0; i<db_getsize()-1;i++)
			{
				printf("%d]\n", i);
				printf("date: %s!\n", dbase.data[i].date);
				printf("requestor: %s!\n", dbase.data[i].requestor);
				printf("email: %s!\n", dbase.data[i].email);
				printf("priority: %s!\n", dbase.data[i].priority);
				printf("needed: %s!\n", dbase.data[i].needed);
				printf("status: %s!\n", dbase.data[i].status);
				printf("description: %s!\n", dbase.data[i].description);
			}
		*/
		if (fW==1) db_update();
		return dbase.pos;
	}
	else
	{
		return -1;
	}
}

void db_update()
{
	fstor=fopen("./Request","a+");

	fprintf(fstor, "Date: %s\n",dbase.data[dbase.pos].date);
	fprintf(fstor, "Requestor: %s\n",dbase.data[dbase.pos].requestor);
	fprintf(fstor, "Email: %s\n",dbase.data[dbase.pos].email);
	fprintf(fstor, "Priority: %s\n",dbase.data[dbase.pos].priority);
	fprintf(fstor, "Needed: %s\n",dbase.data[dbase.pos].needed);
	fprintf(fstor, "Status: %s\n",dbase.data[dbase.pos].status);
	fprintf(fstor, "Description: %s\n",dbase.data[dbase.pos].description);
	fclose(fstor);
}

int db_show_record(int id, ARecord* rec)
{
	if (id<=dbase.pos)
	{
		*rec=dbase.data[id];
		return 0;
	}
	else 
	{	
		rec=NULL;
		return -1;
	}
}

int  db_getsize()
{
	return (dbase.pos+1);
}

int db_search(char* req, ARecord* result)
{
	int i;
	
	
	for (i=0; i<db_getsize();i++)
	{
		/* Removed the trailing newline character 
		
		printf("before %s!\n", dbase.data[i].requestor);
		dbase.data[i].requestor[strlen(dbase.data[i].requestor)-1] = NULL;
		printf("after %s!\n", dbase.data[i].requestor); */
		chomp(dbase.data[i].requestor);
		printf("searching %s, found %s!\n", req, dbase.data[i].requestor);
		if(strcmp(dbase.data[i].requestor,req)==0)
		{
			printf("result found\n");
			return i;
		}
		else
			printf("doesn't match %s, found %s!\n", req, dbase.data[i].requestor);
	}
		
	return -1;
}

void db_close()
{
}


void chomp(char *s) {
while(*s && *s != '\n' && *s != '\r') s++;

*s = 0;
}

